package com.ruoyi.web.dev;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallback;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;

@Component
public class MqttClientConfiguration implements MqttCallback {
//    private MqttClient client;
//    private String broker = "tcp://broker.emqx.io:1883";
//    private String clientId = "publish111";
////  a发布主题
//    private String publicTopic = "publish111/topic";
//    private Integer qos = 2;
//
//    @PostConstruct
//    public void connect() {
//
//        try {
//            client = new MqttClient(broker, clientId);
//
////          MQTT 连接选项
//            MqttConnectOptions connOpts = new MqttConnectOptions();
////          connOpts.setUserName("test111");
////          connOpts.setPassword("test111".toCharArray());
//
////          建立连接
//            client.connect(connOpts);
////          设置回调
//            client.setCallback(new OnMessageCallback() {
//                @Override
//                public void connectionLost(Throwable cause) {
//                    System.out.println("Connection lost! Attempting to reconnect...");
//                }
//                @Override
//                public void messageArrived(String topic, MqttMessage message) throws Exception {
//                    // subscribe后得到的消息会执行到这里面
//                    System.out.println("接收消息主题:" + topic);
//                    System.out.println("接收消息Qos:" + message.getQos());
//                    System.out.println("接收消息内容:" + new String(message.getPayload()));
//                }
//                @Override
//                public void deliveryComplete(IMqttDeliveryToken token) {
////                    System.out.println("发送成功---------" + token.isComplete());
//                }
//            });
//            // 消息发布所需参数
//            MqttMessage message = new MqttMessage("{mm:'你好，我给你说个事'}".getBytes());
//            message.setQos(qos);
//            client.publish(publicTopic, message);
//            System.out.println("Message published");
//
//        } catch (MqttException me) {
//            System.out.println("reason " + me.getReasonCode());
//            System.out.println("msg " + me.getMessage());
//            System.out.println("loc " + me.getLocalizedMessage());
//            System.out.println("cause " + me.getCause());
//            System.out.println("excep " + me);
//            me.printStackTrace();
//        }
//    }
//

    @Override
    public void connectionLost(Throwable throwable) {

    }

    @Override
    public void messageArrived(String s, MqttMessage mqttMessage) throws Exception {

    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken iMqttDeliveryToken) {

    }
}